import { createSlice } from '@reduxjs/toolkit'

// 步骤2: 定义用户数据切片;
const userSlice = createSlice({
    name: 'user',
    initialState: {
        count: 0,
        userinfo: {
            username: '李云龙',
            age: 30
        }
    },
    reducers: {
        addCount(state) {
            state.count++;
        },
        addAge(state, action) {
            // console.log('action', action);
            state.userinfo.age = state.userinfo.age + action.payload
        }

    }
})

// 步骤3:从切片userSlice 中导出修改改切片数据的两个方法
// 后面用来在页面中导入该如下方法修改数据
export const { addCount, addAge } = userSlice.actions
export default userSlice